<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目名称" prop="projectId">
      <el-tag>
        {{dataForm.projectName}}
      </el-tag>
    </el-form-item>
    <el-form-item label="专家名称" prop="expertId">
      <el-tag>
        {{dataForm.expertName}}
      </el-tag>
    </el-form-item>
    <el-form-item label="专家评分" prop="mark">
      <el-select v-model="dataForm.mark" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="专家评价" prop="description">
      <el-input v-model="dataForm.description" placeholder="专家评价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        options: [{
          value: '1',
          label: '1分'
        }, {
          value: '2',
          label: '2分'
        }, {
          value: '3',
          label: '3分'
        }, {
          value: '4',
          label: '4分'
        }, {
          value: '5',
          label: '5分'
        }],
        visible: false,
        dataForm: {
          id: 0,
          projectId: '',
          expertId: '',
          mark: '',
          description: ''
        },
        dataRule: {
          mark: [
            { required: true, message: '专家评分不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '专家评价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/score/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.projectId = data.score.projectId
                this.dataForm.projectName = data.score.projectName
                this.dataForm.expertId = data.score.expertId
                this.dataForm.expertName = data.score.expertName
                this.dataForm.mark = data.score.mark
                this.dataForm.description = data.score.description
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/score/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'projectId': this.dataForm.projectId,
                'projectName': this.dataForm.projectName,
                'expertId': this.dataForm.expertId,
                'expertName': this.dataForm.expertName,
                'mark': this.dataForm.mark,
                'description': this.dataForm.description
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
